package com.fick.tags;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.dao.VodDAO;
import com.fick.hibernate.model.Channel;
import com.fick.hibernate.model.Vod;
import com.fick.util.General;
import com.fick.util.Pager;
/**
 * 显示指定栏目视频列表
 * @author 王浩强
 * 2009-08-18
 * 
 * **/
public class VodListTag extends TagSupport {
	private static Log log = LogFactory.getLog(VodListTag.class);
	//显示几条
	private int num=10;
	//标题长度
	private int length=30;
	//table属性
	private String tableAttr="";
	//tr属性
	private String trAttr="";
	//dot td 属性
	private String dotTdAttr="";
	//title td 属性
	private String titleTdAttr="";
	//date td 属性
	private String dateTdAttr="";
	//dot 图片
	private String dotSrc="";
	//dot 图片属性
	private String dotAttr="";
	//<a>标签href页面
	private String linkPage="";
	//<a>标签属性
	private String linkAttr="";
	//是否显示日期
	private boolean isDate=true;
	//日期格式
	private String dateFormat="yyyy-MM-dd";
	//分页表格table属性
	private String pageTableAttr="";
	//分布表格td属性
	private String pageTdAttr="";
	public int doStartTag() throws JspException {

		int currpage = 1;
		int totalpage=0;
		int totalcount=0;
		int channelid=0;
		int pagesize=20;
		
		try {
			if (num>0) {
				pagesize=num;
			}
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String channelidS = request.getParameter("channelid");
			if (channelidS!=null && channelidS.trim().length()>0) {
				channelid=Integer.parseInt(channelidS);
			}
			Channel channel=new ChannelDAO().findById(channelid);
			StringBuffer sb = new StringBuffer();
			VodDAO dao=new VodDAO();
			totalcount=dao.count(Vod.class, "where  channel.channelId="+channelid);

			//得到总的页数
			if(totalcount%pagesize==0){
				totalpage = totalcount/pagesize;
			}else{
				totalpage = totalcount/pagesize+1;
			}
			if (request.getParameter("currPage") != null) {
				currpage = Integer.parseInt(request.getParameter("currPage"));
			   if(currpage<1){
				     currpage=1;
				    }
				   if(currpage >totalpage){
				     currpage = totalpage;
				   }
			}
			List<Vod> list=dao.findPageBySql(Vod.class,currpage, num,"where channel.channelId="+channelid+" order by addTime desc");
			if (channel!=null && list!=null && list.size()>0) {
				Vod vod;
				sb.append("<table ");
				sb.append(tableAttr);
				sb.append(" ><tbody>");
				for (int i = 0; i < list.size(); i++) {
					vod=list.get(i);
					sb.append("<tr ");
					sb.append(trAttr);
					sb.append(" >");
					sb.append("<td ");
					sb.append(dotTdAttr);
					sb.append(" >");
					if (dotSrc.trim().length()>0) {
						sb.append("<img src='");
						sb.append(dotSrc);
						sb.append("' ");
						sb.append(dotAttr);
						sb.append(" />");
					}
					sb.append("</td>");
					sb.append("<td ");
					sb.append(titleTdAttr);
					sb.append(" ><a href='");
					sb.append(linkPage);
					sb.append("?channelid=");
					sb.append(channelid);
					sb.append("&vodid=");
					sb.append(vod.getId());
					sb.append("' ");
					sb.append(linkAttr);
					sb.append(" >");
					sb.append(General.trimStringInLength(vod.getName(), length));
					sb.append("</a>");
					sb.append("</td>");
					if (isDate) {
						sb.append("<td ");
						sb.append(dateTdAttr);
						sb.append(" >");
						SimpleDateFormat df = new SimpleDateFormat(dateFormat);
						sb.append(df.format(vod.getAddTime()));
						sb.append("</td>");
					}
					sb.append("</tr>");
				}
				sb.append("</tbody></table>");
				//显示分页代码
				sb.append("<table ");
				sb.append(pageTableAttr);
				sb.append(" >");
				sb.append("<tr>");
				sb.append("<td ");
				sb.append(pageTdAttr);
				sb.append(" >");
				sb.append(Pager.nprintTable(channel.getUrl()+"?channelid="+channelid, currpage, totalpage, totalcount, "&",""));
				sb.append("</td>");
				sb.append("</tr>");
				sb.append("</table>");
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		}catch(IOException e)
		{
			log.error("标签显示错误"+e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getTableAttr() {
		return tableAttr;
	}
	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}
	public String getTrAttr() {
		return trAttr;
	}
	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}
	public String getDotTdAttr() {
		return dotTdAttr;
	}
	public void setDotTdAttr(String dotTdAttr) {
		this.dotTdAttr = dotTdAttr;
	}
	public String getTitleTdAttr() {
		return titleTdAttr;
	}
	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}
	public String getDateTdAttr() {
		return dateTdAttr;
	}
	public void setDateTdAttr(String dateTdAttr) {
		this.dateTdAttr = dateTdAttr;
	}
	public String getDotSrc() {
		return dotSrc;
	}
	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}
	public String getDotAttr() {
		return dotAttr;
	}
	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}
	public String getLinkPage() {
		return linkPage;
	}
	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}
	public String getLinkAttr() {
		return linkAttr;
	}
	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}
	public boolean getIsDate() {
		return isDate;
	}
	public void setIsDate(boolean isDate) {
		this.isDate = isDate;
	}
	public String getDateFormat() {
		return dateFormat;
	}
	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
	public String getPageTableAttr() {
		return pageTableAttr;
	}
	public void setPageTableAttr(String pageTableAttr) {
		this.pageTableAttr = pageTableAttr;
	}
	public String getPageTdAttr() {
		return pageTdAttr;
	}
	public void setPageTdAttr(String pageTdAttr) {
		this.pageTdAttr = pageTdAttr;
	}
	
}
